package cn.edu.scau.vsac.service.impl;

import cn.edu.scau.vsac.dao.HelpRequestDao;
import cn.edu.scau.vsac.domain.dto.HelpRequestDto;
import cn.edu.scau.vsac.entity.HelpRequest;
import cn.edu.scau.vsac.service.HelpRequestService;
import cn.edu.scau.vsac.util.HostHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 我要求助表（help_request）(HelpRequest)表服务实现类
 *
 * @author makejava
 * @since 2023-08-05 20:12:18
 */
@Service("helpRequestService")
public class HelpRequestServiceImpl extends ServiceImpl<HelpRequestDao, HelpRequest> implements HelpRequestService {

    @Autowired
    HostHolder hostHolder;

    @Override
    public List<HelpRequest> getPageByUserId(Long userId) {
        QueryWrapper<HelpRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.list(queryWrapper);
    }

    @Override
    public HelpRequest create(HelpRequestDto helpRequestDto) {
        HelpRequest helpRequest = new HelpRequest();
        helpRequest.setAddress(helpRequestDto.getAddress());
        helpRequest.setContent(helpRequestDto.getContent());
        helpRequest.setUserId(hostHolder.getUser().getUserId());
        helpRequest.setName(hostHolder.getUser().getUsername());
        boolean save = this.save(helpRequest);
        if (save) {
            return helpRequest;
        }
        return null;
    }

    @Override
    public String deleteById(Long id) {
        boolean b = this.removeById(id);
        if (b) return "delete success";
        return null;
    }

    @Override
    public HelpRequest getOneById(Long id) {
        HelpRequest byId = this.getById(id);
        if (byId != null) {
            return byId;
        }
        return null;
    }
}

